package cn.bupt.adt.component;

import java.util.Date;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.entity.autogenerate.DoctorAppUser;
import cn.bupt.adt.entity.autogenerate.Finance;
import cn.bupt.adt.entity.autogenerate.InstitutionAppUser;
import cn.bupt.adt.entity.autogenerate.InstitutionView;
import cn.bupt.adt.entity.autogenerate.Recharge;
import cn.bupt.adt.entity.autogenerate.RechargeLog;
import cn.bupt.adt.service.IDoctorAppUserService;
import cn.bupt.adt.service.IFinanceService;
import cn.bupt.adt.service.IInstitutionAppUserService;
import cn.bupt.adt.service.IRechargeLogService;
import cn.bupt.adt.service.IRechargeService;
@Component
public class DealPayUtil {

	@Autowired
	IDoctorAppUserService doctorAppUserService;
	
	@Autowired
	IInstitutionAppUserService institutionAppUserService;
	
	public IDoctorAppUserService getDoctorAppUserService() {
		return doctorAppUserService;
	}
	public void setDoctorAppUserService(IDoctorAppUserService doctorAppUserService) {
		this.doctorAppUserService = doctorAppUserService;
	}
	public IInstitutionAppUserService getInstitutionAppUserService() {
		return institutionAppUserService;
	}
	public void setInstitutionAppUserService(IInstitutionAppUserService institutionAppUserService) {
		this.institutionAppUserService = institutionAppUserService;
	}
	public static DealPayUtil dealPayUtil;
	
	@PostConstruct
	public void init(){
		dealPayUtil = this;
		dealPayUtil.doctorAppUserService = this.doctorAppUserService;
		dealPayUtil.institutionAppUserService = this.institutionAppUserService;
	}
	public static void dealPay(IRechargeService rechargeService,IRechargeLogService rechargeLogService,IFinanceService financeService,int rechargeId,String trade_no,Boolean payType)  {
		if(!rechargeLogService.isExist(trade_no)) {
			Recharge recharge = rechargeService.getRechargeById(rechargeId);
			recharge.setStatus(Constants.HAS_PAYED);
			rechargeService.updateRechargeByPrimaryKeySelective(recharge);
			Finance finacne = null;
			if(recharge.getFlag().equals(Constants.DOC_CHARGE)) {
				DoctorAppUser doc = dealPayUtil.doctorAppUserService.getDoctorById(recharge.getUserId());
				//增加风险保证金
				doc.setRiskMargin(doc.getRiskMargin()+recharge.getMoney());
				dealPayUtil.doctorAppUserService.updateDoctorAppuser(doc);
				finacne = initializeFinance(recharge, doc, true);
			}
			else {
				InstitutionAppUser ins = dealPayUtil.institutionAppUserService.getInstitutionById(recharge.getUserId());
				ins.setMoney(ins.getMoney()+recharge.getMoney());
				dealPayUtil.institutionAppUserService.updateInstitutionAppUser(ins);
				InstitutionView insView = dealPayUtil.institutionAppUserService.getInstitutionViewById(ins.getId());
				finacne = initializeFinance(recharge, insView, false);
			}
			RechargeLog rechargeLog = new RechargeLog();
			rechargeLog.setRechargeId(recharge.getId());
			rechargeLog.setTime(new Date());
			rechargeLog.setTradeNo(trade_no);
			rechargeLog.setType(payType);
			rechargeLogService.addRechargeLog(rechargeLog);
			financeService.addFinance(finacne);
		}
	}
	/**
	 * 
	 * @param recharge 充值记录
	 * @param obj 代表机构或者医生
	 * @param flag true表示obj代表医生，false表示代表机构
	 */
	private static Finance initializeFinance(Recharge recharge,Object obj,boolean flag) {
		Finance finacne = new Finance();
		finacne.setFlag(Constants.INCOME);
		finacne.setMoney(recharge.getMoney());
		finacne.setTime(new Date());
		if(flag) {
			DoctorAppUser doc = (DoctorAppUser) obj;
			finacne.setName(doc.getName());
		}
		else {
			InstitutionView ins = (InstitutionView) obj;
			finacne.setName(ins.getName());
		}
		return finacne;
	}
}
